//Drive_IC:SSD1306
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

#include <reg51.h>
#include <intrins.h>
#include "misc.h"
#include "oled.h"


#define Uchar unsigned char
#define Uint  unsigned int
#define Ulong unsigned long
#define uchar unsigned char
#define uint  unsigned int


//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//  Pin Definition
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=



//sbit SCL3 = P3^0;				// Serial Clock Input
//sbit SDA3 = P3^1;				// Serial Data Input
uchar LCD_x,LCD_y;              // 0 <= x < 128  ; 0 <= y < 8

code fonts[]={
/* [�ֿ�]��[ASC8x16E�ֿ�] [��������]:�����Ҵ��ϵ��� [ȡģ��ʽ]:����8���¸�λ [������ɫ]:�� [ȥ���ظ���]��38���ַ�
[���ַ���]��".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZo"*/

/*-- ID:0,�ַ�:".",ASCII����:2E,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,

/*-- ID:1,�ַ�:"0",ASCII����:30,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xF0,0xF8,0x08,0x88,0x48,0xF8,0xF0,0x00,0x0F,0x1F,0x12,0x11,0x10,0x1F,0x0F,0x00,

/*-- ID:2,�ַ�:"1",ASCII����:31,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x00,0x20,0x30,0xF8,0xF8,0x00,0x00,0x00,0x00,0x10,0x10,0x1F,0x1F,0x10,0x10,0x00,

/*-- ID:3,�ַ�:"2",ASCII����:32,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x10,0x18,0x08,0x88,0xC8,0x78,0x30,0x00,0x1C,0x1E,0x13,0x11,0x10,0x18,0x18,0x00,

/*-- ID:4,�ַ�:"3",ASCII����:33,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x10,0x18,0x88,0x88,0x88,0xF8,0x70,0x00,0x08,0x18,0x10,0x10,0x10,0x1F,0x0F,0x00,

/*-- ID:5,�ַ�:"4",ASCII����:34,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x80,0xC0,0x60,0x30,0xF8,0xF8,0x00,0x00,0x01,0x01,0x01,0x11,0x1F,0x1F,0x11,0x00,

/*-- ID:6,�ַ�:"5",ASCII����:35,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xF8,0xF8,0x88,0x88,0x88,0x88,0x08,0x00,0x08,0x18,0x10,0x10,0x11,0x1F,0x0F,0x00,

/*-- ID:7,�ַ�:"6",ASCII����:36,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xE0,0xF0,0x98,0x88,0x88,0x80,0x00,0x00,0x0F,0x1F,0x10,0x10,0x10,0x1F,0x0F,0x00,

/*-- ID:8,�ַ�:"7",ASCII����:37,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x18,0x18,0x08,0x08,0x88,0xF8,0x78,0x00,0x00,0x00,0x1E,0x1F,0x01,0x00,0x00,0x00,

/*-- ID:9,�ַ�:"8",ASCII����:38,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x70,0xF8,0x88,0x88,0x88,0xF8,0x70,0x00,0x0F,0x1F,0x10,0x10,0x10,0x1F,0x0F,0x00,

/*-- ID:10,�ַ�:"9",ASCII����:39,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x70,0xF8,0x88,0x88,0x88,0xF8,0xF0,0x00,0x00,0x10,0x10,0x10,0x18,0x0F,0x07,0x00,

/*-- ID:11,�ַ�:"A",ASCII����:41,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xC0,0xE0,0x30,0x18,0x30,0xE0,0xC0,0x00,0x1F,0x1F,0x01,0x01,0x01,0x1F,0x1F,0x00,

/*-- ID:12,�ַ�:"B",ASCII����:42,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x08,0xF8,0xF8,0x88,0x88,0xF8,0x70,0x00,0x10,0x1F,0x1F,0x10,0x10,0x1F,0x0F,0x00,

/*-- ID:13,�ַ�:"C",ASCII����:43,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xE0,0xF0,0x18,0x08,0x08,0x18,0x30,0x00,0x07,0x0F,0x18,0x10,0x10,0x18,0x0C,0x00,

/*-- ID:14,�ַ�:"D",ASCII����:44,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x08,0xF8,0xF8,0x08,0x18,0xF0,0xE0,0x00,0x10,0x1F,0x1F,0x10,0x18,0x0F,0x07,0x00,

/*-- ID:15,�ַ�:"E",ASCII����:45,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x08,0xF8,0xF8,0x88,0xC8,0x18,0x38,0x00,0x10,0x1F,0x1F,0x10,0x11,0x18,0x1C,0x00,

/*-- ID:16,�ַ�:"F",ASCII����:46,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x08,0xF8,0xF8,0x88,0xC8,0x18,0x38,0x00,0x10,0x1F,0x1F,0x10,0x01,0x00,0x00,0x00,

/*-- ID:17,�ַ�:"G",ASCII����:47,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xE0,0xF0,0x18,0x08,0x08,0x18,0x30,0x00,0x07,0x0F,0x18,0x11,0x11,0x0F,0x1F,0x00,

/*-- ID:18,�ַ�:"H",ASCII����:48,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xF8,0xF8,0x80,0x80,0x80,0xF8,0xF8,0x00,0x1F,0x1F,0x00,0x00,0x00,0x1F,0x1F,0x00,

/*-- ID:19,�ַ�:"I",ASCII����:49,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x00,0x00,0x08,0xF8,0xF8,0x08,0x00,0x00,0x00,0x00,0x10,0x1F,0x1F,0x10,0x00,0x00,

/*-- ID:20,�ַ�:"J",ASCII����:4A,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x00,0x00,0x00,0x08,0xF8,0xF8,0x08,0x00,0x0E,0x1E,0x10,0x10,0x1F,0x0F,0x00,0x00,

/*-- ID:21,�ַ�:"K",ASCII����:4B,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x08,0xF8,0xF8,0x80,0xE0,0x78,0x18,0x00,0x10,0x1F,0x1F,0x01,0x03,0x1E,0x1C,0x00,

/*-- ID:22,�ַ�:"L",ASCII����:4C,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x08,0xF8,0xF8,0x08,0x00,0x00,0x00,0x00,0x10,0x1F,0x1F,0x10,0x10,0x18,0x1C,0x00,

/*-- ID:23,�ַ�:"M",ASCII����:4D,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xF8,0xF8,0x70,0xE0,0x70,0xF8,0xF8,0x00,0x1F,0x1F,0x00,0x00,0x00,0x1F,0x1F,0x00,

/*-- ID:24,�ַ�:"N",ASCII����:4E,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xF8,0xF8,0x70,0xE0,0xC0,0xF8,0xF8,0x00,0x1F,0x1F,0x00,0x00,0x01,0x1F,0x1F,0x00,

/*-- ID:25,�ַ�:"O",ASCII����:4F,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xE0,0xF0,0x18,0x08,0x18,0xF0,0xE0,0x00,0x07,0x0F,0x18,0x10,0x18,0x0F,0x07,0x00,

/*-- ID:26,�ַ�:"P",ASCII����:50,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x08,0xF8,0xF8,0x88,0x88,0xF8,0x70,0x00,0x10,0x1F,0x1F,0x10,0x00,0x00,0x00,0x00,

/*-- ID:27,�ַ�:"Q",ASCII����:51,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xF0,0xF8,0x08,0x08,0x08,0xF8,0xF0,0x00,0x0F,0x1F,0x10,0x1C,0x78,0x7F,0x4F,0x00,

/*-- ID:28,�ַ�:"R",ASCII����:52,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x08,0xF8,0xF8,0x88,0x88,0xF8,0x70,0x00,0x10,0x1F,0x1F,0x00,0x01,0x1F,0x1E,0x00,

/*-- ID:29,�ַ�:"S",ASCII����:53,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x30,0x78,0xC8,0x88,0x88,0x38,0x30,0x00,0x0C,0x1C,0x10,0x10,0x11,0x1F,0x0E,0x00,

/*-- ID:30,�ַ�:"T",ASCII����:54,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x00,0x38,0x18,0xF8,0xF8,0x18,0x38,0x00,0x00,0x00,0x10,0x1F,0x1F,0x10,0x00,0x00,

/*-- ID:31,�ַ�:"U",ASCII����:55,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xF8,0xF8,0x00,0x00,0x00,0xF8,0xF8,0x00,0x0F,0x1F,0x10,0x10,0x10,0x1F,0x0F,0x00,

/*-- ID:32,�ַ�:"V",ASCII����:56,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xF8,0xF8,0x00,0x00,0x00,0xF8,0xF8,0x00,0x03,0x07,0x0C,0x18,0x0C,0x07,0x03,0x00,

/*-- ID:33,�ַ�:"W",ASCII����:57,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0xF8,0xF8,0x00,0x00,0x00,0xF8,0xF8,0x00,0x07,0x1F,0x1C,0x07,0x1C,0x1F,0x07,0x00,

/*-- ID:34,�ַ�:"X",ASCII����:58,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x18,0x78,0xE0,0x80,0xE0,0x78,0x18,0x00,0x18,0x1E,0x07,0x01,0x07,0x1E,0x18,0x00,

/*-- ID:35,�ַ�:"Y",ASCII����:59,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x00,0x78,0xF8,0x80,0x80,0xF8,0x78,0x00,0x00,0x00,0x10,0x1F,0x1F,0x10,0x00,0x00,

/*-- ID:36,�ַ�:"Z",ASCII����:5A,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x38,0x18,0x08,0x88,0xC8,0x78,0x38,0x00,0x1C,0x1E,0x13,0x11,0x10,0x18,0x1C,0x00,

/*-- ID:37,�ַ�:"o",ASCII����:6F,��Ӧ��:��x��=8x16,����:��W=8 ��H=16,��16�ֽ�*/
0x00,0x00,0x0C,0x12,0x12,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

// ID:38   " "
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};


//****************************************************************************
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//  Read/Write Sequence
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void I2C_3(Uchar mcmd)
{
Uchar length = 8;			// Send Command

	while(length--)
	{
		if(mcmd & 0x80)
		{
			SDA3=1;
		}
		else
		{
			SDA3=0;
		}
//		uDelay(3);
		SCL3=1;
//		uDelay(3);
		SCL3=0;
//		uDelay(3);
		mcmd = mcmd << 1;
	}
}


void I2C_Ack3()
{
	SDA3=1;
//	uDelay(3);
	SCL3=1;
//	uDelay(3);
	SCL3=0;
//	uDelay(3);
}


void I2C_Start3()
{
	SDA3=0;
//	uDelay(3);
	SCL3=1;
//	uDelay(3);
	SCL3=0;
//	uDelay(3);
	I2C_3(0x78);
	I2C_Ack3();
}


void I2C_Stop3()
{
	SCL3=1;
//	uDelay(5);
	SDA3=0;
//	uDelay(5);
	SDA3=1;
//	uDelay(5);
}


void Write_Command3(Uchar Data)
{
	I2C_Start3();
	I2C_3(0x00);
	I2C_Ack3();
	I2C_3(Data);
	I2C_Ack3();
	I2C_Stop3();
}


void Write_Data3(Uchar Data)
{
	I2C_Start3();
	I2C_3(0x40);
	I2C_Ack3();
	I2C_3(Data);
	I2C_Ack3();
	I2C_Stop3();
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

void	LCD_Init3(void)
{
//RES3=0;
//uDelay(2);
//RES3=1;
//internal setting
    LCD_x=0;
    LCD_y=0;

Write_Command3(0xae);	//--turn off oled panel

Write_Command3(0x00);	//--set low column address
Write_Command3(0x10);	//--set high column address

Write_Command3(0x40);	//--set start line address

Write_Command3(0xb0);	//--set page address

Write_Command3(0x81);	//--set contrast control register
Write_Command3(0xff);

Write_Command3(0xa1);	//--set segment re-map 127 to 0   a0:0 to seg127
Write_Command3(0xa6);	//--set normal display

Write_Command3(0xc8);	//--set com(N-1)to com0  c0:com0 to com(N-1)

Write_Command3(0xa8);	//--set multiples ratio(1to64)
Write_Command3(0x3f);	//--1/64 duty

Write_Command3(0xd3);	//--set display offset
Write_Command3(0x00);	//--not offset

Write_Command3(0xd5);	//--set display clock divide ratio/oscillator frequency
Write_Command3(0x80);	//--set divide ratio

Write_Command3(0xd9);	//--set pre-charge period
Write_Command3(0xf1);

Write_Command3(0xda);	//--set com pins hardware configuration
Write_Command3(0x12);

Write_Command3(0xdb);	//--set vcomh
Write_Command3(0x40);

Write_Command3(0x8d);	//--set chare pump enable/disable
Write_Command3(0x14);	//--set(0x10) disable
//Write_Command3(0x10);	//--set(0x10) disable
Write_Command3(0xaf);	//--turn on oled panel
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=void 	LCD_Init(void)
}

void LCD_Clean()
{
	Uchar j,i;

    Write_Command3(0x21);//--set col
    Write_Command3(0);//--set start col
    Write_Command3(127);//--set end col
    
    for(i=0;i<8;i++)
    {
        Write_Command3(0x22);//--set page1
        Write_Command3(i);//--set start page
        Write_Command3(0x07);//--set end page

        for(j=0;j<128;j++)
        {
                Write_Data3(0);
        }
    }
}

void LCD_Putc(uchar ch)
{
    uint i,j,k;
    
    if(LCD_x>127 || LCD_y>7)return;

    if(ch=='.')k=0;
    else if(ch>='0' && ch<='9') k=(1+(ch-'0'))*16;
    else if(ch>='A' && ch<='Z') k=(11 + (ch-'A'))*16;
    else if(ch=='o') k=37*16;
    else k=38*16;
    
    for(i=0;i<2;i++)
    {
        Write_Command3(0x22);//--set page1
        Write_Command3(LCD_y+i);//--set start page
        Write_Command3(7);//--set end page

        Write_Command3(0x21);//--set col
        Write_Command3(LCD_x);//--set start col
        Write_Command3(127);//--set end col
  
        for(j=0;j<8;j++)
        {
            Write_Data3(fonts[k++]);
        }
    }
    LCD_x+=8;
}
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void LCD_Print(uchar *s,uint num,uchar x,uchar y)
{
    int i;
    
    LCD_x=x-1;
    LCD_y=y;
    LCD_Putc(' ');  //ȥ���ƶ������Ĳ�Ӱ
    
    LCD_x=x;
    while(*s)
    {
        LCD_Putc(*s++);
    }
    for(i=100;i>0;num=num-(num/i)*i,i/=10)
    {
        LCD_Putc((num/i)+'0');
        if(i==10)LCD_Putc('.');
    }
    
    LCD_Putc('o');
    LCD_Putc('C');

}
